import axios from '../libs/api.request'
import { Message, Notification } from 'element-ui'

export const requestSend = (act, params) => {
    let data = params || {};
    return axios.request({
        url: act,
        data: data,
        method: 'post'
    })
};

export const request = (act, params) => {
    return new Promise((resolve, reject) => {
        requestSend(act, params).then(res => {
            const {
                data,
                status
            } = res
            if(data.code === 422) {
                Message.error({
                    message:data.msg
                })
            } else if(data.code === -1 || data.code > 200) {
                if (data.data) {
                    Notification.warning({
                        title: data.msg,
                        message: data.data
                    });
                } else {
                    Notification.error({
                        title: '['+status+']' + data.msg
                    });
                }
                reject(data);
            } else {
                resolve(data)
            }
        }).catch(e => {
            Notification.error({
                title: 'Request Error',
                message: e
            })
            reject(e)
        })
    })
};
